Ext.define('Admin.app.CrudController', {
	        extend : 'Admin.app.BaseController',

	        /**
			 * 合并记录到store中
			 */
	        merge : function(store, record) {
		        var result = store.find('id', record.data.id);
		        if (-1 < result) { // 合并属性
			        result = store.getAt(result);
			        Ext.apply(result.data, record.data);
			        result.commit();
		        } else { // 新建记录
			        result = store.add(record.data)[0];
		        }
		        store.reload();
		        return result;
	        },

	        /**
			 * 复制一条记录的内容，用于编辑修改，，否则无法撤销修改
			 */
	        duplicate : function(record) {
		        var duplicate = Ext.create(record.store.model, Ext.apply({}, record.data));
		        duplicate.store = record.store;
		        return duplicate;
	        },

	        onOk : function(button) {
		        var me = this, form = button.ownerCt.ownerCt, win = form.ownerCt, vm = win.getViewModel();
		        var record = vm.get('record'), store = record.store;
		        me.beforeSave && Ext.callback(me.beforeSave, me, [record]);
		        Admin.Ajax.submit(form, {
			                scope : me,
			                mask : win,
			                fnSuccess : function(options, result) {
				                var ret = me.merge(store, result);
				                me.afterSave && Ext.callback(me.afterSave, options.scope, [options, ret]);
				                win.close();
			                }
		                }, record);
	        },

	        onCancel : function(button) {
		        var win = button.ownerCt.ownerCt.ownerCt;
		        win.close();
	        },

	        showWin : function(form, title, fnOk) {
		        var me = this;
		        var win = Ext.widget({
			                xtype : 'detailwin',
			                renderTo : Ext.getBody(),
			                viewModel : true,
			                title : title,
			                items : [{
				                        xtype : form,
				                        buttons : [{
					                                text : '确定',
					                                ui : 'soft-green',
					                                iconCls : 'x-fa fa-check fa-lg',
					                                reference : 'btnOk',
					                                scope : me,
					                                handler : fnOk
				                                }, {
					                                ui : 'gray',
					                                text : '取消',
					                                scope : me,
					                                reference : 'btnCancel',
					                                handler : me.onCancel
				                                }]
			                        }]
		                });
		        return win;
	        },

	        showDetail : function(detail, record) {
		        var me = this, record = me.duplicate(record);
		        var win = me.showWin(detail, record.getText(), me.onOk);
		        win.getViewModel().set('record', record);
		        me.afterDetail(record, win);
		        win.show();
	        },

	        onCellDblClick : function(view, td, cellIndex, record, tr, rowIndex, e, eOpts) {
		        var grid = view.ownerCt, headerCt = grid.getHeaderContainer();
		        var column = headerCt.getHeaderAtIndex(cellIndex);
		        if (0 == cellIndex || 'checkcolumn' === column.xtype) { // 所有的check框，都不能产生双击效果
			        return;
		        }
		        this.showDetail(grid.detail, record);
	        },

	        onDeleteClick : function(button, e) {
		        var me = this, grid = button.ownerCt.ownerCt, records = grid.getSelection();
		        Admin.Ajax.drop(records, {
			                grid : grid,
			                scope : me,
			                afterDrop : me.afterDrop
		                });
	        },

	        onTestClick : function(button, e) {
	        	Ext.Ajax.request({
	        		url:'rest/api/trade/test',
	        		success:function(){
	        			alert('请求后台成功')
	        		},
	        		failure:function(){
	        			alert('请求后台失败')
	        		}
	        	})
	        },
	        
	        onEditActionClick : function(view, rowIndex, colIndex, item, e, record, row) {
		        var grid = view.ownerCt;
		        this.showDetail(grid.detail, record);
	        },

	        onDeleteActionClick : function(view, rowIndex, colIndex, item, e, record, row) {
		        var me = this, records = [];
		        records.push(record);
		        Admin.Ajax.drop(records, {
			                grid : view,
			                scope : me,
			                afterDrop : me.afterDrop
		                });
	        },

	        afterDrop : function(option, records) {
		        // TODO 删除后的操作
	        },

	        afterDetail : function(record, win) {
		        // TODO 弹出编辑框后的操作
	        }

        });